package top.xana.acg.music.ui.page

import android.content.Context
import android.media.Image
import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import android.util.AttributeSet
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.Adapter
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.Toast
import androidx.coordinatorlayout.widget.CoordinatorLayout
import top.xana.acg.common.base.adp.RecyclerAdapter
import top.xana.acg.common.base.ui.BaseFragment
import top.xana.acg.common.utils.radius
import top.xana.acg.common.utils.toast
import top.xana.acg.music.R
import top.xana.acg.music.data.Music
import top.xana.acg.music.databinding.DailyRecommendFragmentBinding
import kotlin.math.abs

class DailyRecommendFragment : BaseFragment() {

    companion object {
        const val TAG = "DailyRecommendFragment"
        fun newInstance() = DailyRecommendFragment()
    }

    private lateinit var viewModel: DailyRecommendViewModel


    override fun getLayoutId() = R.layout.daily_recommend_fragment

    private val b by lazy {
        getBinding<DailyRecommendFragmentBinding>()
    }
    override fun initView() {
        super.initView()

        b.playListCardView.setAdapter(PlayListAdapter().also { it.addAll(List(30) { Music() }) })
    }

    override fun onResume() {
        super.onResume()
        val maxY = requireContext().resources.getDimension(R.dimen.dp_66)
    }


    class Behavior(context: Context? = null, attrs: AttributeSet? = null) :
        CoordinatorLayout.Behavior<View>(context, attrs) {


        var sx = 0f
        var sy = 0f
        var maxY = context!!.resources.getDimension(R.dimen.dp_76)

        override fun onInterceptTouchEvent(
            parent: CoordinatorLayout,
            child: View,
            ev: MotionEvent
        ) = when(ev.action){
                MotionEvent.ACTION_DOWN-> {
                    sx = ev.x
                    sy = ev.y
                    false
                }

                MotionEvent.ACTION_MOVE->{

                    Log.e(TAG, "y:${-child.y}, maxY:$maxY")

                    if(-child.y<=maxY) {
                        val dy = ev.y-sy
                        child.y += dy.toInt()
                        sy = ev.y
                        val mh = child.measuredHeight
                        val lp = child.layoutParams
                        lp.height = mh-dy.toInt()
                        child.layoutParams = lp

                    }

                    false

                }
            else-> super.onInterceptTouchEvent(parent, child, ev)
            }

        override fun onTouchEvent(
            parent: CoordinatorLayout,
            child: View,
            ev: MotionEvent
        ): Boolean {
            return -child.y<=maxY
        }



        private var originHeight = 0
        override fun onNestedScroll(
            coordinatorLayout: CoordinatorLayout,
            child: View,
            target: View,
            dxConsumed: Int,
            dyConsumed: Int,
            dxUnconsumed: Int,
            dyUnconsumed: Int,
            type: Int,
            consumed: IntArray
        ) {
//            Log.e(TAG, "onNestedScroll: ${dyConsumed}")
        }


        override fun onStartNestedScroll(
            coordinatorLayout: CoordinatorLayout,
            child: View,
            directTargetChild: View,
            target: View,
            axes: Int,
            type: Int
        ): Boolean {
            return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type)
        }

    }


    class PlayListAdapter : RecyclerAdapter<Music>() {

        init {
            listener = object : RecyclerAdapter.OnItemListenerImpl<Music>() {
                override fun onItemLongClick(holder: ViewHolder<Music>, data: Music) {
                    super.onItemLongClick(holder, data)
                    holder.itemView.toast("长按了Item")
                }
            }
        }

        override fun getItemViewType(position: Int, data: Music) =
            R.layout.layout_item_play_list_bar


        override fun onCreateViewHolder(view: View, viewType: Int): ViewHolder<Music> {
            view.findViewById<ImageView>(R.id.iv_mv).setOnClickListener(this)
            view.findViewById<ImageView>(R.id.iv_ext).setOnClickListener(this)

            return object : RecyclerAdapter.ViewHolder<Music>(view) {
                override fun onBind(data: Music) {
                }
            }
        }

        override fun onClick(v: View) {
            when (v.id) {
                R.id.iv_mv -> println("点击了MV")
                R.id.iv_ext -> println("点击了EXT")
                else -> super.onClick(v)
            }
        }
    }

}